#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in_systm.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <string>
#include "definitions.hpp"


/* default snap length (maximum bytes per packet to capture) */
#define SNAP_LEN 1518

/* ethernet headers are always exactly 14 bytes [1] */
#define SIZE_ETHERNET 14

/* Ethernet addresses are 6 bytes */
//#define ETHER_ADDR_LEN	6




void
got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{

	static int count = 1;                   /* packet counter */

    uint16_t size;

	bzero(&size,2);
    memcpy(&size,packet+14,2);

	printf("%d",size);

	string str((const char*)(packet+16),50); 
	printf("%s",str.c_str());

	if (str.find("RED") != string::npos)
	{	  
      packet_q_i1.push_back((unsigned char*)packet);
	} else if (str.find("BLUE") != string::npos)
	{
      packet_q_i2.push_back((unsigned char*)packet);
	} else if (str.find("GREEN") != string::npos)
	{
      packet_q_i3.push_back((unsigned char*)packet);
	} else if (str.find("YELLOW") != string::npos)
	{
      packet_q_i4.push_back((unsigned char*)packet);
	} else {

	}
    
    printf("adding to q");
	printf("\nPacket number received from front %d:\n", count);
	count++;

	/* define ethernet header */
return;
}

void* sniffer_thread_func(void* val)
{
	int* i  = (int*) val;
	list<string>::iterator it;
	it = interface_list.begin();
	for(int k=1; k < (*i); k++){
		++it;
	}

	char *dev = (char *)((*it).c_str()); /* capture device name */
	char errbuf[PCAP_ERRBUF_SIZE];		/* error buffer */
	pcap_t *handle;				/* packet capture handle */

	char filter_exp[] = "ip";		/* filter expression [3] */
	struct bpf_program fp;			/* compiled filter program (expression) */
	bpf_u_int32 mask;			/* subnet mask */
	bpf_u_int32 net;			/* ip */
	int num_packets = -1;			/* number of packets to capture */

		if (dev == NULL) {
			fprintf(stderr, "Couldn't find default device: %s\n",
			    errbuf);
			exit(EXIT_FAILURE);
		}

	/* get network number and mask associated with capture device */
	if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
		fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
		    dev, errbuf);
		net = 0;
		mask = 0;
	}

	/* print capture info */
	printf("Device: %s\n", dev);
	printf("Number of packets: %d\n", num_packets);
	printf("Filter expression: %s\n", filter_exp);


		/* open capture device */
		handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
		if (handle == NULL) {
			fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
			exit(EXIT_FAILURE);
		}


		/* make sure we're capturing on an Ethernet device [2] */
		if (pcap_datalink(handle) != DLT_EN10MB) {
			fprintf(stderr, "%s is not an Ethernet\n", dev);
			exit(EXIT_FAILURE);
		}

		/* compile the filter expression
		if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
			fprintf(stderr, "Couldn't parse filter %s: %s\n",
			    filter_exp, pcap_geterr(handle));
			exit(EXIT_FAILURE);
		}*/

		/* apply the compiled filter
		if (pcap_setfilter(handle, &fp) == -1) {
			fprintf(stderr, "Couldn't install filter %s: %s\n",
			    filter_exp, pcap_geterr(handle));
			exit(EXIT_FAILURE);
		}*/

		/* now we can set our callback function */
		pcap_loop(handle, num_packets, got_packet, NULL);

		/* cleanup */
		pcap_freecode(&fp);
		pcap_close(handle);

		printf("\nCapture complete.\n");


return 0;
}

